﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Reflection;
using System.IO;

namespace littleAdminHelper
{
    public partial class frm_settings : Form
    {
        public frm_settings()
        {
            InitializeComponent();
            //load Default Settings from Config File
            load_configuration();


            //MessageBox.Show(Application.StartupPath);

            open_general_logo.InitialDirectory = Path.Combine(Application.StartupPath, @"ext");



        }

        private void chk_remotesupport_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_general_remotesupport.Checked)
            {
                txt_general_remotesupporturl.Enabled = true;
                ConfigurationManager.AppSettings.Set("remoteHelp", "true");
                
            }
            else {
                txt_general_remotesupporturl.Enabled = false;
                ConfigurationManager.AppSettings.Set("remoteHelp", "false");
            }
        }

        private void load_configuration()
        {
            // load General Settings
            if (Boolean.Parse(ConfigurationManager.AppSettings.Get("showLogo")))
            {
                chk_general_logo.Checked = true;
            }
            else
            {
                chk_general_logo.Checked = false;
            }
            //load Logopath
            txt_general_logopath.Text = ConfigurationManager.AppSettings.Get("logopath");



            if (Boolean.Parse(ConfigurationManager.AppSettings.Get("remoteHelp")))
            {
                chk_general_remotesupport.Checked=true;
                txt_general_remotesupporturl.Enabled=true;
            }else{
                chk_general_remotesupport.Checked=false;
                txt_general_remotesupporturl.Enabled=false;
            }
            txt_general_remotesupporturl.Text = ConfigurationManager.AppSettings.Get("remoteHelpUrl");


            // load Mail Settings
            if (Boolean.Parse(ConfigurationManager.AppSettings.Get("MaildeliveryEnabled")))
            {
                chk_mail_enablemailsmtp.Checked = true;

            }
            else
            {
                chk_mail_enablemailsmtp.Checked = false;
            }

            try
            {
                txt_mail_smtpserver.Text = ConfigurationManager.AppSettings.Get("SmtpServer");
                chk_mail_smtpauth.Checked = Boolean.Parse(ConfigurationManager.AppSettings.Get("SmtpAuthRequired"));
                chk_mail_ssl.Checked = Boolean.Parse(ConfigurationManager.AppSettings.Get("SmtpSSLEnable"));
                txt_mail_smtpauthuser.Text = ConfigurationManager.AppSettings.Get("SmtpAuthUser");
                txt_mail_smtpauthpassword.Text = SimpleCrypto.encrypt(ConfigurationManager.AppSettings.Get("SmtpAuthPassword"));
                //MessageBox.Show(txt_mail_smtpauthpassword.Text);
                //MessageBox.Show(ConfigurationManager.AppSettings.Get("SmtpAuthPassword"));
                txt_mail_senderaddress.Text = ConfigurationManager.AppSettings.Get("SupportSenderaddress");
                txt_mail_subject.Text = ConfigurationManager.AppSettings.Get("SupportEmailSubject");
                txt_mail_text.Text = ConfigurationManager.AppSettings.Get("SupportEmailText");
            }
            catch (Exception e){
                
            }

            // load scripts

            try
            {
                // Load Scripts from Configuration File
                for (int i = 0; i<7; i++)
                {
                    lst_script_scriptlist.Items.Add(load_script_config(i));
                }
            }
            catch (Exception e)
            {
                //
            }

            // load WMI Querys

            try
            {
                for (int i = 0; i<7 ; i++)
                {
                    lst_wmi_querylist.Items.Add(load_wmi_config(i));
                }
            }
            catch (Exception e)
            {
            }


            // load Registry Querys
            try 
            {
                for (int i = 0; i<7; i++)
                {
                    lst_registry_settings.Items.Add(load_registry_config(i));
                }

            }
            catch (Exception e)
            {
                MessageBox.Show("Loading Registry Data is not Possible!");
            }

            // load application Data
            try
            {
                for (int i = 0; i < 7; i++)
                {
                    lst_applications_applist.Items.Add(load_application_config(i));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Loading Application Data is not Possible!");
            }

            

        }

        private object load_registry_config(int index)
        {
            String output = "";
            try
            {
                output += ConfigurationManager.AppSettings.Get(index + "RegistryQueryEnable") + "|";
                output += ConfigurationManager.AppSettings.Get(index + "RegistryPath") + "|";
                output += ConfigurationManager.AppSettings.Get(index + "RegistryValue") + "|";
                output += ConfigurationManager.AppSettings.Get(index + "RegistryResultDescription") + "|";
                output += ConfigurationManager.AppSettings.Get(index + "RegistryShowInComputerInfo") + "|";
                output += ConfigurationManager.AppSettings.Get(index + "RegistryShowInNetworkInfo");
                
            }
            catch (Exception ex)
            {
                return null;
            }

            return output;
        }

        private void save_configuration()
        {
            string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string configFile = System.IO.Path.Combine(appPath, "littleAdminHelper.exe.config");
            ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
            configFileMap.ExeConfigFilename = configFile;


            
            System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
            
            // save GeneralSettings
            // save Logo
            config.AppSettings.Settings["showLogo"].Value = "" + chk_general_logo.Checked;
            config.AppSettings.Settings["logopath"].Value= txt_general_logopath.Text;


            config.AppSettings.Settings["remoteHelp"].Value = "" + chk_general_remotesupport.Checked;
            config.AppSettings.Settings["remoteHelpUrl"].Value = txt_general_remotesupporturl.Text;
            //ConfigurationManager.AppSettings.Set("remoteHelpUrl", txt_remotesupporturl.Text);

            // save MailSettings
            config.AppSettings.Settings["MaildeliveryEnabled"].Value = "" + chk_mail_enablemailsmtp.Checked;
            config.AppSettings.Settings["SmtpServer"].Value = txt_mail_smtpserver.Text;
            config.AppSettings.Settings["SmtpAuthRequired"].Value = "" + chk_mail_smtpauth.Checked;
            config.AppSettings.Settings["SmtpSSLEnable"].Value = "" + chk_mail_ssl.Checked;
            config.AppSettings.Settings["SmtpAuthUser"].Value = txt_mail_smtpauthuser.Text;
            config.AppSettings.Settings["SmtpAuthPassword"].Value = SimpleCrypto.decrypt(txt_mail_smtpauthpassword.Text);
            config.AppSettings.Settings["SupportSenderaddress"].Value = txt_mail_senderaddress.Text;
            config.AppSettings.Settings["SupportEmailSubject"].Value = txt_mail_subject.Text;
            config.AppSettings.Settings["SupportEmailText"].Value = txt_mail_text.Text;


            // save ScriptConfiguration to Config File
            try
            {
                String allvalues="";
                String[] values;

                for (int i = 0; true; i++)
                {
                allvalues = lst_script_scriptlist.Items[i].ToString();
                values = allvalues.Split('|');

                config.AppSettings.Settings[i +"Scriptsrc"].Value= values[0];
                config.AppSettings.Settings[i +"ScripthasResult"].Value= values[1];
                config.AppSettings.Settings[i +"ScriptshowInOverview"].Value = values[2];
                config.AppSettings.Settings[i +"ScriptresultCode"].Value = values[3];
                config.AppSettings.Settings[i +"ScriptOverviewDescription"].Value = values[4];
                config.AppSettings.Settings[i +"ScriptShowInURLMenue"].Value = values[5];
                config.AppSettings.Settings[i +"ScriptURLMenuDescription"].Value = values[6];

                }
            }
            catch (Exception ex)
            {
                //keine Scripts mehr vorhanden
            }

            // save WMI Querys to Config File
            try
            {
                String allvalues = "";
                String[] values;

                for (int i = 0; true ; i++)
                {
                    allvalues = lst_wmi_querylist.Items[i].ToString();
                    values = allvalues.Split('|');

                    config.AppSettings.Settings[i + "WMIQueryEnable"].Value = values[0];
                    config.AppSettings.Settings[i + "WMIQueryDirectory"].Value = values[1];
                    config.AppSettings.Settings[i + "WMIQuery"].Value = values[2];
                    config.AppSettings.Settings[i + "WMIResultDescription"].Value = values[3];
                    config.AppSettings.Settings[i + "WMIShowinComputerInfo"].Value = values[4];
                    config.AppSettings.Settings[i + "WMIShowinNetworkInfo"].Value = values[5];
                }
            } catch (Exception ex){

                // no more Config Settings
            }

            // save Registry Settings to Config File
            try
            {
                String allValues = "";
                String[] values;
                for (int i = 0; true; i++)
                {
                    allValues = lst_registry_settings.Items[i].ToString();
                    values = allValues.Split('|');

                    config.AppSettings.Settings[i + "RegistryQueryEnable"].Value = values[0];
                    config.AppSettings.Settings[i + "RegistryPath"].Value = values[1];
                    config.AppSettings.Settings[i + "RegistryValue"].Value = values[2];
                    config.AppSettings.Settings[i + "RegistryResultDescription"].Value = values[3];
                    config.AppSettings.Settings[i + "RegistryShowInComputerInfo"].Value = values[4];
                    config.AppSettings.Settings[i + "RegistryShowInNetworkInfo"].Value = values[5];
                }

            }
            catch (Exception ex)
            {
                // no more Config Settings
            }
           

            try
            {
                String allvalues = "";
                String[] values;
                for (int i = 0; true; i++)
                {
                    allvalues = lst_applications_applist.Items[i].ToString();
                    values = allvalues.Split('|');

                    config.AppSettings.Settings[i + "ApplicationEnable"].Value = values[0];
                    config.AppSettings.Settings[i + "ApplicationSrc"].Value = values[1];
                    config.AppSettings.Settings[i + "ApplicationDescription"].Value = values[2];
                }


            }
            catch
            {
                // no more Config Settings
            }




            config.Save(ConfigurationSaveMode.Modified);
            
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            save_configuration();
            this.Close();
        }

        private void txt_general_remotesupporturl_TextChanged(object sender, EventArgs e)
        {

        }

        private void btn_general_openlogo_Click(object sender, EventArgs e)
        {
            open_general_logo.ShowDialog();
            txt_general_logopath.Text = PathConversion.decodepath(open_general_logo.FileName.ToString());
        }

        private void btn_wmi_add_Click(object sender, EventArgs e)
        {
            // defaultEntry
            lst_wmi_querylist.Items.Add("\\root\\CIMV2|SELECT IdentifyingNumber FROM Win32_ComputerSystemProduct|Seriennummer: |true|false");

        }

        private void lst_wmi_querylist_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                String allvalues = lst_wmi_querylist.Items[lst_wmi_querylist.SelectedIndex].ToString();
                String[] values = allvalues.Split('|');

                chk_wmi_query.Checked = Boolean.Parse(values[0]);
                txt_wmi_directory.Text = values[1];
                txt_wmi_query.Text = values[2];
                txt_wmi_wmidescription.Text = values[3];
                chk_wmi_showinnetwork.Checked = Boolean.Parse(values[4]);
                chk_wmi_showincomputerinfo.Checked = Boolean.Parse(values[5]);
            }
            catch (Exception ex)
            {
                chk_wmi_query.Checked = false;
                txt_wmi_directory.Text = "";
                txt_wmi_query.Text = "";
                txt_wmi_wmidescription.Text = "";
                chk_wmi_showincomputerinfo.Checked = false;
                chk_wmi_showinnetwork.Checked = false;
            }




            //MessageBox.Show(values);
        }

        private void btn_script_open_Click(object sender, EventArgs e)
        {
            open_scriptDialogue.ShowDialog();
            txt_script_filename.Text = PathConversion.decodepath(open_scriptDialogue.FileName.ToString());
        }

        private void btn_wmi_remove_Click(object sender, EventArgs e)
        {
            try
            {
                lst_wmi_querylist.Items.RemoveAt(lst_wmi_querylist.SelectedIndex);
            }
            catch (Exception ex)
            {
            }
        }

        private String load_script_config(int index)
        {
            String output = "";
            try
            {
                output += ConfigurationManager.AppSettings.Get(index + "Scriptsrc") + "|";
                output += ConfigurationManager.AppSettings.Get(index + "ScripthasResult") + "|";
                output += ConfigurationManager.AppSettings.Get(index + "ScriptshowInOverview") + "|";
                output += ConfigurationManager.AppSettings.Get(index + "ScriptresultCode") + "|";
                output += ConfigurationManager.AppSettings.Get(index + "ScriptOverviewDescription") + "|";
                output += ConfigurationManager.AppSettings.Get(index + "ScriptShowInURLMenue") + "|";
                output += ConfigurationManager.AppSettings.Get(index + "ScriptURLMenuDescription") + "|";
                output += ConfigurationManager.AppSettings.Get(index + "ScriptEnable");
            }
            catch (Exception ex)
            {
                return null;
            }

            return output;
        }

        private String load_wmi_config(int index)
        {
            String output = "";
            try
            {
               
                    output += ConfigurationManager.AppSettings.Get(index + "WMIQueryEnable") + "|";
                    output += ConfigurationManager.AppSettings.Get(index + "WMIQueryDirectory") + "|";
                    output += ConfigurationManager.AppSettings.Get(index + "WMIQuery") + "|";
                    output += ConfigurationManager.AppSettings.Get(index + "WMIResultDescription") + "|";
                    output += ConfigurationManager.AppSettings.Get(index + "WMIShowinComputerInfo") + "|";
                    output += ConfigurationManager.AppSettings.Get(index + "WMIShowinNetworkInfo");
               
                return output;
            }
            catch {
            // do nothing
                return null;
            }


        }

        private String load_application_config(int index)
        {
            String output = "";
            try
            {

                output += ConfigurationManager.AppSettings.Get(index + "ApplicationEnable") + "|";
                output += ConfigurationManager.AppSettings.Get(index + "ApplicationSrc") + "|";
                output += ConfigurationManager.AppSettings.Get(index + "ApplicationDescription") + "|";

                return output;
            }
            catch
            {
                // do nothing
                return null;
            }


        }

        private void lst_script_scriptlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                String allvalues = lst_script_scriptlist.Items[lst_script_scriptlist.SelectedIndex].ToString();
                String[] values = allvalues.Split('|');

                txt_script_filename.Text = values[0];
                chk_script_hasresult.Checked = Boolean.Parse(values[1]);
                chk_script_showinoverview.Checked = Boolean.Parse(values[2]);
                txt_script_resultcodeforok.Text = values[3];
                txt_script_overviewdescription.Text = values[4];
                chk_script_showinurlmenu.Checked = Boolean.Parse(values[5]);
                txt_script_urlmenudescription.Text = values[6];
                chk_script_enable.Checked=Boolean.Parse(values[7]);
            }
            catch (Exception ex)
            {
                txt_script_filename.Text = "";
                chk_script_hasresult.Checked = false;
                chk_script_showinoverview.Checked = false;
                txt_script_resultcodeforok.Text = "";
                txt_script_overviewdescription.Text = "";
                chk_script_showinurlmenu.Checked = false;
                txt_script_urlmenudescription.Text = "";
                chk_script_enable.Checked = false;
            }
        }

        private void btn_application_open_Click(object sender, EventArgs e)
        {
            open_applicationDialogue.ShowDialog();
            txt_application_file.Text = PathConversion.decodepath(open_applicationDialogue.FileName.ToString());
        }

        private void chk_application_isapplication_CheckedChanged(object sender, EventArgs e)
        {
           
        }

        private void btn_application_add_Click(object sender, EventArgs e)
        {
            if (txt_application_file.Text != "")
            {
                lst_applications_applist.Items.Add(txt_application_file.Text + "|" + txt_application_description.Text);
            }
            else
            {

            }
        }

        private void lst_applications_applist_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                String allvalues = lst_applications_applist.Items[lst_applications_applist.SelectedIndex].ToString();
                String[] values = allvalues.Split('|');


                chk_application_enable.Checked = Boolean.Parse(values[0]);
                txt_application_file.Text = values[1];
                txt_application_description.Text = values[2];

            }
            catch (Exception ex) { }
        }

        private void btn_script_save_Click(object sender, EventArgs e)
        {
            // alter values in Script List
            String allvalues = "";

            allvalues += txt_script_filename.Text + "|";
            allvalues += chk_script_hasresult.Checked + "|";
            allvalues += chk_script_showinoverview.Checked + "|";
            allvalues += txt_script_resultcodeforok.Text + "|";
            allvalues += txt_script_overviewdescription.Text + "|";
            allvalues += chk_script_showinurlmenu.Checked + "|";
            allvalues += txt_script_urlmenudescription.Text + "|";
            allvalues += chk_script_enable.Checked;

            try
            {
                // write Files to List
                lst_script_scriptlist.Items[lst_script_scriptlist.SelectedIndex] = allvalues;
            }
            catch (Exception ex)
            {
                // do nothing
            }


        }

        private void btn_wmi_save_Click(object sender, EventArgs e)
        {

            String allvalues = "";

            allvalues += chk_wmi_query.Checked + "|";
            allvalues += txt_wmi_directory.Text + "|";
            allvalues += txt_wmi_query.Text + "|";
            allvalues += txt_wmi_wmidescription.Text + "|";
            allvalues += chk_wmi_showinnetwork.Checked + "|";
            allvalues += chk_wmi_showincomputerinfo.Checked;

            // write to List File
            try
            {
                lst_wmi_querylist.Items[lst_wmi_querylist.SelectedIndex] = allvalues;

            }
            catch
            {
                // do nothing
            }




        }

        private void chk_wmi_showincomputerinfo_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void lst_registry_settings_SelectedIndexChanged(object sender, EventArgs e)
        {
            try {
            String allvalues = lst_registry_settings.Items[lst_registry_settings.SelectedIndex].ToString();
            String[] values = allvalues.Split('|');
            chk_registry_enable.Checked = Boolean.Parse(values[0]);
            txt_registry_path.Text = values[1];
            txt_registry_valuename.Text = values[2];
            txt_registry_resultdescription.Text = values[3];
            chk_registry_showincomputerinfo.Checked = Boolean.Parse(values[4]);
            chk_registry_showinnetwork.Checked = Boolean.Parse(values[5]);

            }
            catch (Exception ex){
                chk_registry_enable.Checked =false;
                txt_registry_path.Text = "";
                txt_registry_valuename.Text = "";
                txt_registry_resultdescription.Text = "";
                chk_registry_showincomputerinfo.Checked = false;
                chk_registry_showinnetwork.Checked = false;
            }
            


        }

        private void btn_registry_save_Click(object sender, EventArgs e)
        {
            String output = "" + chk_registry_enable.Checked + "|";
            output += txt_registry_path.Text + "|";
            output += txt_registry_valuename.Text + "|";
            output += txt_registry_resultdescription.Text + "|";
            output += chk_registry_showincomputerinfo.Checked + "|";
            output += chk_registry_showinnetwork.Checked;

            lst_registry_settings.Items[lst_registry_settings.SelectedIndex] = output;
        }

        private void btn_application_save_Click(object sender, EventArgs e)
        {
            String output = "";
            output += chk_application_enable.Checked + "|";
            output += txt_application_file.Text + "|";
            output += txt_application_description.Text;
            try
            {
                lst_applications_applist.Items[lst_applications_applist.SelectedIndex] = output;
            }
            catch (Exception ex)
            {
                lst_applications_applist.Items.Add(output);
            }



        }

       

    }
}
